import dt from '@dt/dt'
import orderApi from '@/config/server/api/order.js'
import userApi from '@/config/server/api/user.js'
import { Message } from 'view-design'

let positionList = []
let search = null

function list(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.level.list.fetch(info)
}

function listAll(info) {
  return orderApi.bonus.level.listAll.fetch(info)
}

function detail(rankPointsId) {
  return orderApi.bonus.level.detail.fetch({ rankPointsId })
}

function save(info) {
  info.housesId = dt.buildInfo().housesId
  return orderApi.bonus.level.save.fetch(info)
}

function repeat(name) {
  return orderApi.bonus.level.repeat.fetch({
    housesId: dt.buildInfo().housesId,
    name
  })
}

function del(rankPointsId) {
  return orderApi.bonus.level.del.fetch({ rankPointsId })
}

// 获取参与奖金的岗位列表
function bonusPosition() {
  return userApi.jobPostManagement.bonusPosition.fetch().then(res => {
    let data = res.map(i => ({
      key: i.id,
      value: i.positionName
    }))
    positionList = data
    return data
  })
}

// 获取参与奖金对应的职级列表
function bonusLevel() {
  return userApi.jobPostManagement.bonusLevel.fetch()
}

function positions() {
  return positionList
}

function validateNum(val) {
  if (!val && val !== 0) {
    return false
  }
  return true
}

function validateMention(data) {
  let flag = true, reg = /^\d+$/
  if (!validateNum(data.mentionSomethingPercentage)) {
    flag = false
    Message.error('提点设置不能为空')
  } else if (data.mentionSomethingType == '1' && !reg.test(data.mentionSomethingPercentage)) {
    flag = false
    Message.error('提点类型为“折算比例”时，提点值应为整数')
  }
  return flag
}

// 校验区间
function onInterval(list) {
  let flag = true,
      msg = '区间数据有误，请检查'
  
  // 首先校验单个区间是否有误
  for (let item of list) {
    let start = item.startValue, end = item.endValue
    if (!validateNum(start) || !validateNum(end)) {
      flag = false
      msg = '区间数据不能为空'
    } else if ((start > end) || (start == end && (item.startInterval != '2' || item.endInterval != '2'))) {
      // 开始结束值相等时，必须是100 <= v <= 100
      flag = false
    }
    
    if (!flag) {
      Message.error(msg)
      break
    } else if (!validateMention(item)) {
      flag = false
      break
    }
  }
  
  // 单个区间校验通过，再校验区间是否有重叠
  if (flag) {
    flag = onOverlap(list)
  }
  return flag
}

// 校验区间是否有重叠
function onOverlap(list) {
  let flag = true
      
  for (let item of list) {
    let start = item.startValue, end = item.endValue,
        startI = item.startInterval, endI = item.endInterval
    let index = list.findIndex(i => i == item)
    let data = list.slice(index + 1)
    console.log('item', start, end)
    
    for (let it of data) {
      let s = it.startValue, e = it.endValue,
          sI = it.startInterval, eI = it.endInterval
      console.log(s, e)
      if (start == end) {
        if ((s == start && sI == '2') || (e == start && eI == '2') || (s < start && e > start)) {
          flag = false
        }
      } else {
        if (s == e) {
          if ((s == start && startI == '2') || (s == end && endI == '2') || (s > start && s < end)) {
            flag = false
          }
        } else if (
          (s >= start && s < end)
          || (e > start && e <= end)
          || (s < start && e > end)
          || (s == end && endI == '2' && sI == '2')
          || (e == start && startI == '2' && eI == '2')
        ) {
          flag = false
        }
      }
      if (!flag) break
    }
    console.log(flag)
    if (!flag) {
      Message.error('区间数据有误，请检查')
      break
    }
  }
  return flag
}

// 校验排名
function onRanking(list) {
  let flag = true,
      reg = /^\d+$/
  for (let item of list) {
    if (!item.startValue || !reg.test(item.startValue)) {
      flag = false
      Message.error('排名应为正整数')
      break
    } else if (!validateMention(item)) {
      flag = false
      break
    }
  }
  return flag
}

function onValidateDetail(info) {
  if (!info.boRankJumpPointDetailInfos || info.boRankJumpPointDetailInfos.length <= 0) {
    Message.error('请添加职级跳点明细')
    return false
  } else if (info.targetValue == '1' && !onInterval(info.boRankJumpPointDetailInfos)) {
    return false
  } else if (info.targetValue == '2' && !onRanking(info.boRankJumpPointDetailInfos)) {
    return false
  }
  return true
}

function onValidateBase(list) {
  let valid = true
  for (let item of list) {
    if (!validateNum(item.bonusRatio)) {
      valid = false
      break
    }
  }
  if (!valid) {
    Message.error('基础职级点数设置有误，请检查并填写完整')
  }
  return valid
}

function onValidateJump(list, flag) {
  let valid = true
  if (flag == 'import') {
    for (let item of list) {
      if (!item.productIds || !item.productIds.length) {
        valid = false
        break
      }
    }
  }
  if (!valid) {
    Message.error('请完善职级跳点中产品信息')
  }
  return valid
}

function saveSearch(params) {
  search = params
}

function getSearch(params) {
  return search
}

export default {
  list,
  listAll,
  detail,
  save,
  repeat,
  del,
  
  bonusPosition,
  bonusLevel,
  positions,
  onValidateJump,
  onValidateBase,
  onValidateDetail,
  saveSearch,
  getSearch
}